<?php

class ContractUnlocker
{
    public static function isContractEditable($contract){
        if($contract->status == 3 || $contract->invoiceCount > 0){
            return false;
        }else{
            return true;
        }
    }

    public static function canRenderUnlockButton()
    {
        $user = Users::model()->findByPk(Yii::app()->user->id);
        return $user->checkPermission('contracts/unlock');
    }

    public static function unlockContract($contractid)
    {
        if (self::canRenderUnlockButton())
        {
            $_SESSION['contract-unlock'] = array(
                'contract'   => $contractid,
                'view'      => 0
            );
        }
        else
        {
            if (isset($_SESSION['contract-unlock']))
            {
                unset($_SESSION['contract-unlock']);
            }
        }
    }

    public static function lockAllContract()
    {
        if (isset($_SESSION['contract-unlock']))
        {
            unset($_SESSION['contract-unlock']);
        }
    }

    public static function checkAndLockContract($contractid)
    {
        if (isset($_SESSION['contract-unlock']))
        {
            if ($_SESSION['contract-unlock']['contract'] == $contractid && $_SESSION['contract-unlock']['view'] >= 2)
            {
                unset($_SESSION['contract-unlock']);
            }
        }
    }

    public static function increaseUnlockContractViewCount($contractid)
    {
        if (isset($_SESSION['contract-unlock']))
        {
            if ($_SESSION['contract-unlock']['contract'] == $contractid)
            {
                $_SESSION['contract-unlock']['view']++;
                if ($_SESSION['contract-unlock']['view'] >= 2)
                {
                    unset($_SERVER['contract-unlock']);
                }
            }
        }
    }

    public static function isContractUnlocked($contractid)
    {
        if (isset($_SESSION['contract-unlock']))
        {
            if ($_SESSION['contract-unlock']['contract'] == $contractid && $_SESSION['contract-unlock']['view'] < 2)
            {
                return true;
            }
            else
            {
                
            }
        }
        return false;
    }

}

?>
